import json

import httpx
from utils.logger import get_logger

logger = get_logger()

def notify(data, callback_url, task_id, status: str):
    logger.info(data)
    try:
        responese = httpx.post(callback_url, json={"task_id": task_id,
                                                   "status": status,
                                                   "metaData": data},
                               headers={"Content-Type": "application/json"})
        if responese.status_code != 201:
            logger.error("error to notify")
        # logger.info(responese.json())
    except:
        import traceback
        logger.error(f"post callback url failed{traceback.format_exc()}")

def parse_s3url(url: str):
    bucket_name, object_name = url[len("s3://"):].split('/', 1)
    return bucket_name, object_name

def read_metadata(storage, bucket_name, object_name):
    data = storage.get_object(bucket_name, object_name)
    metadata = json.loads(data.read())
    return metadata
